import {getMusicLyric,getMusicVkey} from "../../api/Singer"
import {ERR_OK} from "../../api/config"
import {Base64} from "js-base64"
// 这里创建一个Song类
export default class Song {
    constructor({
        id,//歌曲的id
        mid,
        singer,
        name,//歌曲名称
        album,//专辑名称
        duration,//歌曲的时长，方便后面做播放器
        image,//歌曲的图片
        url,//歌曲的请求路径
    }){
        this.id = id;
        this.mid = mid;
        this.singer = singer;
        this.name = name;
        this.album = album;
        this.duration = duration;
        this.image = image;
        this.url = url;
    }
     //请求获取歌曲的歌词
    getMusic_Lyric(type){
        if(this.lyric){
            // return Promise.resolve(this.lyric)
            return Promise.resolve()
        }
        return new Promise((resolve,reject) => {
            getMusicLyric(this.mid).then((res)=>{
                if(res.code === ERR_OK){
                     this.lyric  = Base64.decode(res.lyric) // 解码 得到字符串
                     resolve(this.lyric)
                }else{
                     reject('no lyric')
                }
            })
        })
     }

}

export function  createSong(musicData,pcurl){
    return new Song({
        id: musicData.songid,
        mid: musicData.songmid,
        singer: fliterSinger(musicData.singer),
        name: musicData.songname,
        album: musicData.albumname,
        duration: musicData.interval,
        image: concatSongImg(musicData.albummid),
        url: concatSongUrl(pcurl)
    })
}

// 如果歌手名是两个或以上，就用/分开
 function fliterSinger (singer){
    let ret = []
    if(!singer){
        return ''
    }
    singer.forEach((s) => {
        ret.push(s.name)
    });
    return ret.join('/')
}
//拼接歌曲的图片地址
function  concatSongImg (albummid){
    return `https://y.gtimg.cn/music/photo_new/T002R300x300M000${albummid}_2.jpg?max_age=2592000`
}
export function  concatSongUrl (pcurl){
   if(!pcurl){
        return ''
   }else{
    return `http://aqqmusic.tc.qq.com/amobile.music.tc.qq.com/${pcurl}`
   }

}

export function  createHotSong(musicData,pcurl){
    return new Song({
        id:  musicData.id,
        mid: musicData.mid,
        singer: fliterSinger(musicData.singer),
        name: musicData.name,
        album: musicData.album.name,
        duration: musicData.interval,
        image: concatHotSongImg(musicData.album.mid),
        url: concatSongUrl(pcurl)
    })
}

//拼接歌曲的图片地址
function  concatHotSongImg (albummid){
    return `https://y.gtimg.cn/music/photo_new/T002R300x300M000${albummid}_2.jpg?max_age=2592000`
}


export function  createResultSong(musicData,pcurl){
    return new Song({
        id: musicData.id,
        mid: musicData.mid,
        singer: fliterSinger(musicData.singer),
        name: musicData.name,
        album: musicData.album.name,
        duration: musicData.interval,
        image: concatSongImg(musicData.album.mid),
        url: concatSongUrl(pcurl)
    })
}

  // 请求播放链接
export function getVkey(mid){
    getMusicVkey(mid).then( (res) =>{
        if(res.code === ERR_OK){
            return  concatSongUrl(res.req_0.data.midurlinfo[0].purl)
        }else{
            console.log("2")
        }
    } )
}
